package org.weasel.views;

import java.sql.Time;
import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.weasel.business.Iteration;
import org.weasel.business.Task;
import org.weasel.business.UserStory;

public class UserStoryDetail extends ViewPart {
	public static final String ID = "org.weasel.views.UserStoryDetail";

	private Composite top = null;
	private Button button = null;
	private Label userstoryid = null;
	private Label userstoryname = null;
	private Label effort = null;
	private Label businessvalue = null;
	private Label estimatedtime = null;
	private Label usedtime = null;
	private Label developers = null;
	private Label tasks = null;
	private Label userstoryidtext = null;
	private Label userstorynametext = null;
	private Label estimatedtimetext = null;
	private Label usedtimetext = null;
	private Label businessvaluetext = null;
	private Label efforttext = null;
	private Label developerstext = null;
	private Label taskstext = null;
	private Text editestimatedtime = null;
	private Text editusedtime = null;
	private Text editbusinessvalue = null;
	private Text editeffort = null;
	private Text edituserstoryname = null;
	private Text editdevelopers = null;
	private Text edittasks = null;
	private String userstoryID;
	private String usntxt;
	private String esttxt;
	private String usdtmtxt;
	private String bvtxt;
	private String efftxt;
	private Iteration iter;
	private UserStory userstory;
	private String tsktxt;
	private String devtxt;
	private ArrayList<Task> tsk;

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		top = new Composite(parent, SWT.NONE);
		top.setLayout(null);
		button = new Button(top, SWT.NONE);
		button.setBounds(new Rectangle(12, 306, 97, 27));
		button.setVisible(false);
		button.setText("Save");
		button.addMouseListener(new org.eclipse.swt.events.MouseListener() {
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
				button.setVisible(false);
				edituserstoryname.setVisible(false);
				editestimatedtime.setVisible(false);
				editusedtime.setVisible(false);
				editbusinessvalue.setVisible(false);
				editeffort.setVisible(false);
				editdevelopers.setVisible(false);
				edittasks.setVisible(false);
				UserStory us=null;
				
				//userstory
				try {
					iter.removeUserStory(userstoryID);
					us=iter.createAndAddUserStory(usntxt , 
												Integer.parseInt(bvtxt), 
												Time.valueOf(esttxt), 
												Time.valueOf(usdtmtxt), 
												Integer.parseInt(efftxt));

					for (Task task : tsk) {
						iter.removeTask(task.getId());
					}
					
					iter.addTask(us.getId(), tsktxt);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				userstorynametext.setText(usntxt);
				userstorynametext.setVisible(true);
				estimatedtimetext.setText(esttxt);
				estimatedtimetext.setVisible(true);
				usedtimetext.setText(usdtmtxt);
				usedtimetext.setVisible(true);
				businessvaluetext.setText(bvtxt);
				businessvaluetext.setVisible(true);
				efforttext.setText(efftxt);
				efforttext.setVisible(true);
				developerstext.setText("");
				developerstext.setVisible(true);
				taskstext.setText("");
				taskstext.setVisible(true);
				
			
			}
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
			}
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
			}
		});
		
		userstoryid = new Label(top, SWT.NONE);
		userstoryid.setBounds(new Rectangle(10, 10, 70, 16));
		userstoryid.setText("UserStory ID:");
		userstoryname = new Label(top, SWT.NONE);
		userstoryname.setBounds(new Rectangle(180, 9, 85, 17));
		userstoryname.setText("UserStory Name:");
		effort = new Label(top, SWT.NONE);
		effort.setBounds(new Rectangle(180, 60, 40, 15));
		effort.setText("Effort: ");
		businessvalue = new Label(top, SWT.NONE);
		businessvalue.setBounds(new Rectangle(180, 35, 78, 18));
		businessvalue.setText("Business Value:");
		estimatedtime = new Label(top, SWT.NONE);
		estimatedtime.setBounds(new Rectangle(10, 35, 78, 14));
		estimatedtime.setText("Estimated Time:");
		usedtime = new Label(top, SWT.NONE);
		usedtime.setBounds(new Rectangle(10, 60, 59, 14));
		usedtime.setText("Used Time:");
		developers = new Label(top, SWT.NONE);
		developers.setBounds(new Rectangle(350, 95, 131, 15));
		developers.setText("Developers:");
		tasks = new Label(top, SWT.NONE);
		tasks.setBounds(new Rectangle(10, 95, 231, 15));
		tasks.setText("Tasks:");
		userstoryidtext = new Label(top, SWT.NONE);
		userstoryidtext.setBounds(new Rectangle(82, 11, 85, 15));
		userstoryidtext.setText("usid txt");
		userstorynametext = new Label(top, SWT.NONE);
		userstorynametext.setBounds(new Rectangle(271, 10, 232, 16));
		userstorynametext.setText("usn txt");
		userstorynametext.addMouseListener(new org.eclipse.swt.events.MouseListener() {
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
				
				userstorynametext.setVisible(false);
				edituserstoryname.setText(usntxt);
				edituserstoryname.setVisible(true);
				button.setVisible(true);
			}
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
			}
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
			}
		});
		estimatedtimetext = new Label(top, SWT.NONE);
		estimatedtimetext.setBounds(new Rectangle(90, 35, 70, 17));
		estimatedtimetext.setText("est txt");
		estimatedtimetext.addMouseListener(new org.eclipse.swt.events.MouseListener() {
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
				estimatedtimetext.setVisible(false);
				editestimatedtime.setText(esttxt);
				editestimatedtime.setVisible(true);
				button.setVisible(true);
			}
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
			}
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
			}
		});
		usedtimetext = new Label(top, SWT.NONE);
		usedtimetext.setBounds(new Rectangle(90, 60, 70, 17));
		usedtimetext.setText("ut txt");
		usedtimetext.addMouseListener(new org.eclipse.swt.events.MouseListener() {
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
				usedtimetext.setVisible(false);
				editusedtime.setText(usdtmtxt);
				editusedtime.setVisible(true);
				button.setVisible(true);
			}
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
			}
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
			}
		});
		businessvaluetext = new Label(top, SWT.NONE);
		businessvaluetext.setBounds(new Rectangle(264, 35, 30, 17));
		businessvaluetext.setText("bv txt");
		businessvaluetext.addMouseListener(new org.eclipse.swt.events.MouseListener() {
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
				businessvaluetext.setVisible(false);
				editbusinessvalue.setText(bvtxt);
				editbusinessvalue.setVisible(true);
				button.setVisible(true);
				}
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
			}
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
			}
		});
		efforttext = new Label(top, SWT.NONE);
		efforttext.setBounds(new Rectangle(264, 60, 30, 21));
		efforttext.setText("eff txt");
		efforttext.addMouseListener(new org.eclipse.swt.events.MouseListener() {
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
				efforttext.setVisible(false);
				editeffort.setText(efftxt);
				editeffort.setVisible(true);
				button.setVisible(true);
			}
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
			}
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
			}
		});
		developerstext = new Label(top, SWT.NONE);
		developerstext.setBounds(new Rectangle(347, 113, 159, 90));
		developerstext.setText("dev txt");
		developerstext.addMouseListener(new org.eclipse.swt.events.MouseListener() {
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
				developerstext.setVisible(false);
				editdevelopers.setVisible(true);
			}
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
			}
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
			}
		});
		taskstext = new Label(top, SWT.NONE);
		taskstext.setBounds(new Rectangle(9, 111, 288, 159));
		taskstext.setText("task txt");
		taskstext.addMouseListener(new org.eclipse.swt.events.MouseListener() {
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
				taskstext.setVisible(false);
				edittasks.setVisible(true);
				edittasks.setText(taskstext.getText());
				button.setVisible(true);
			}
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
			}
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
			}
		});
		editestimatedtime = new Text(top, SWT.BORDER);
		editestimatedtime.setBounds(new Rectangle(90, 32, 80, 20));
		editestimatedtime.setVisible(false);
		editestimatedtime
				.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
					public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
						esttxt=editestimatedtime.getText();
					}
				});
		editusedtime = new Text(top, SWT.BORDER);
		editusedtime.setBounds(new Rectangle(90, 56, 80, 20));
		editusedtime.setVisible(false);
		editusedtime.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
				usdtmtxt=editusedtime.getText();
			}
		});
		editbusinessvalue = new Text(top, SWT.BORDER);
		editbusinessvalue.setBounds(new Rectangle(264, 34, 41, 20));
		editbusinessvalue.setVisible(false);
		editbusinessvalue
				.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
					public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
						bvtxt=editbusinessvalue.getText();
					}
				});
		editeffort = new Text(top, SWT.BORDER);
		editeffort.setBounds(new Rectangle(264, 60, 40, 20));
		editeffort.setVisible(false);
		editeffort.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
				efftxt=editeffort.getText();
			}
		});
		edituserstoryname = new Text(top, SWT.BORDER);
		edituserstoryname.setBounds(new Rectangle(270, 8, 240, 19));
		edituserstoryname.setVisible(false);
		edituserstoryname
				.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
					public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
						usntxt=edituserstoryname.getText();
					}
				});
		editdevelopers = new Text(top, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		editdevelopers.setBounds(new Rectangle(341, 114, 190, 95));
		editdevelopers.setVisible(false);
		editdevelopers.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
				devtxt=editdevelopers.getText();
			}
		});
		edittasks = new Text(top, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		edittasks.setBounds(new Rectangle(8, 113, 309, 178));
		edittasks.setVisible(false);
		edittasks.addModifyListener(new org.eclipse.swt.events.ModifyListener() {
			public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
				tsktxt=edittasks.getText();
			}
		});
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	/**
	 * This method shows all the details of a user story passed as parameter on
	 * the user story detail view.
	 * @param userStory
	 */
	public void showUserStoryInformation(UserStory userStory) {
		userstoryID = userStory.getId();
		userstoryidtext.setText(userStory.getId());
		userstorynametext.setText(userStory.getName());
		usntxt = userStory.getName();
		estimatedtimetext.setText(userStory.getEstimatedTime().toString());
		esttxt = userStory.getEstimatedTime().toString();
		usedtimetext.setText(userStory.getUsedTime().toString());
		usdtmtxt = userStory.getUsedTime().toString();
		businessvaluetext.setText("" + userStory.getBusinessValue());
		bvtxt = "" + userStory.getBusinessValue();
		efforttext.setText("" + userStory.getEffort());
		efftxt = "" + userStory.getEffort();
		userstory = userStory;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10,545,367"
